﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using UpperComputer.WPF.Entry.Views;
using YFurion.Application;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class RoleViewModel : ManagementViewModel
    {
        private readonly IBaseRoleService _roleService;
        private readonly IBaseUserService _userService;

        public BaseRoleResultRequestDto Query { get; } = new BaseRoleResultRequestDto();

        private PagedResultDto<BaseRoleResultDto> _rolePaged = new PagedResultDto<BaseRoleResultDto>();
        public PagedResultDto<BaseRoleResultDto> RolePaged
        {
            get => _rolePaged;
            set => SetProperty(ref _rolePaged, value);
        }

        private BaseRoleResultDto _currentRole;
        public BaseRoleResultDto CurrentRole
        {
            get => _currentRole;
            set => SetProperty(ref _currentRole, value);
        }

        public RoleViewModel(IBaseRoleService roleService, IBaseUserService userService)
        {
            _roleService = roleService;
            _userService = userService;
        }

        protected override async Task LoadAsync(object parameter, CancellationToken cancellationToken)
        {
            SetCurrent(parameter);
            SetSkipCount(Query);
            RolePaged = await _roleService.GetAll(Query);
            CurrentRole = null;
        }

        protected override async Task PostAsync(CancellationToken cancellationToken)
        {
            RoleOperationControl roleOperationControl = Furion.App.GetService<RoleOperationControl>();
            object result = await ShowRootDialog(roleOperationControl);
            if (result != null)
            {
                await LoadAsync(1, cancellationToken);
            }
        }

        protected override async Task PutAsync(CancellationToken cancellationToken)
        {
            if (CurrentRole == null)
            {
                Snackbar.ShowWarningMessage("请选择一行编辑");
                return;
            }
            RoleOperationControl roleOperationControl = Native.CreateInstance<RoleOperationControl>(CurrentRole.Id);
            object result = await ShowRootDialog(roleOperationControl);
            if (result != null)
            {
                await LoadAsync(null, cancellationToken);
            }
        }

        protected override async Task DeleteAsync(CancellationToken cancellationToken)
        {
            if (CurrentRole == null)
            {
                Snackbar.ShowWarningMessage("请选择一行删除");
                return;
            }
            MessageBoxResult mbr = this.ShowQuestionBox("确定要删除吗");
            if (mbr == MessageBoxResult.OK)
            {
                await _userService.GetUserCount(CurrentRole.Id, cancellationToken);
                await _roleService.Delete(CurrentRole.Id, cancellationToken);
            }
        }

        protected override async Task OperationAsync(string operation, CancellationToken cancellationToken)
        {
            if (CurrentRole == null)
            {
                Snackbar.ShowWarningMessage("请选择一个角色分配权限");
                return;
            }
            RolePermissionsControl rolePermissionsControl = Native.CreateInstance<RolePermissionsControl>(CurrentRole.Id);
            await ShowRootDialog(rolePermissionsControl);
        }
    }
}
